package com.david.viewtest

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.graphics.RectF
import android.os.Bundle
import android.text.Html
import android.text.Layout
import android.text.Spannable
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.davemorrissey.labs.subscaleview.ImageViewState
import com.david.core.utils.loadRoundCornerImg
import com.david.viewtest.view.AvatarImageView
import com.david.viewtest.view.BigImgView
import com.david.viewtest.view.ImageSource
import com.david.viewtest.view.MySubsamplingScaleImageView
import com.david.viewtest.view.TextViewWithLabels
import com.david.viewtest.view.TextViewWithLabelsHtml


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<View>(R.id.nav_anim_float).setOnClickListener {
            navDragFloat()

            //it.rotation = 45f//view旋转45度（还可设置translate/ scale/ alpha），无动画

            //val matrix = Matrix()
            //matrix.setScale(0.5f, 0.5f)
            //findViewById<ImageView>(R.id.round_drawable_img).imageMatrix = matrix//图片缩小1/2，view不变
        }
        //https://zeekrlife-oss.zeekrlife.com/IMAGE/1/manage/2102/zeekrlife-mp-osp/20220701/903d048d61454e79ba78dc9643a90a5b.jpg
        //https://zeekrlife-oss.zeekrlife.com/IMAGE/1/manage/2102/zeekrlife-mp-osp/20220701/f8086fbfb8d141d69b85bcf085aaf722.jpg

        //Glide.with(this)
        //    .asBitmap()
        //    .load("https://zeekrlife-oss.zeekrlife.com/IMAGE/1/manage/2102/zeekrlife-mp-osp/20220701/903d048d61454e79ba78dc9643a90a5b.jpg")
        //    //.circleCrop()
        //    .into(findViewById(R.id.round_drawable_img))

        //findViewById<AvatarImageView>(R.id.testimg).loadCircleImg("https://s0.2mdn.net/simgad/14955103473871160577")
        findViewById<AvatarImageView>(R.id.testimg).loadRoundCornerImg("https://s0.2mdn.net/simgad/14955103473871160577", 100)
            //.into( object : CustomTarget<Bitmap>() {
            //    override fun onResourceReady(
            //        resource: Bitmap,
            //        transition: Transition<in Bitmap>?,
            //    ) {
            //
            //        findViewById<ImageView>(R.id.round_drawable_img).setImageBitmap(getRoundedCornerBitmap(resource, 200f, 30f, 50f, 0f))
            //        //((RoundedDrawable) drawable).setCornerRadius(
            //        //    mCornerRadii[Corner.TOP_LEFT],
            //        //    mCornerRadii[Corner.TOP_RIGHT],
            //        //    mCornerRadii[Corner.BOTTOM_RIGHT],
            //        //    mCornerRadii[Corner.BOTTOM_LEFT]);
            //    }
            //        //if (index < bitmaps.size) {
            //        //    bitmaps.removeAt(index)
            //        //    bitmaps.add(index, resource)
            //        //    postInvalidate()
            //        //}
            //    //}
            //
            //    override fun onLoadCleared(placeholder: Drawable?) {
            //    }
            //})

        //avif没有透明度通道，
        Glide.with(this).load("http://192.168.1.104:8090/files/aviftest.avif").into(findViewById(R.id.aviftest_img))
        findViewById<TextView>(R.id.nav_anim_float)?.let { view ->
            view.post {
               view.text = "${findViewById<BigImgView>(R.id.big_img).imgWidthHeightRatio}"
           }
        }

        findViewById<MySubsamplingScaleImageView>(R.id.scale_image).let {
            it.postDelayed({
                it.setImage(ImageSource.resource(R.mipmap.example), ImageViewState(0f, PointF(0f, 0f), 0))
            }, 1000)
        }

        findViewById<TextViewWithLabels>(R.id.icons_label_view).setContentAndTag(
            "你哈哈哈哈你哈哈哈哈mmmmm毛m你哈哈哈哈你哈哈哈你哈哈哈哈",
            arrayListOf(
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                R.mipmap.ic_prize,
                R.mipmap.ic_hot
            ),
            arrayListOf(
                //R.mipmap.ic_prize,
                //R.mipmap.ic_hot,
                //R.mipmap.ic_hot,
                R.mipmap.ic_hot,
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                R.mipmap.ic_hot,

                )
        )

        findViewById<TextViewWithLabelsHtml>(R.id.textview_html).setContentAndTag("hahahha",
            arrayListOf(
                "https://zeekrlife-oss-test.lkhaowu.com/IMAGE/1/client/1468049848863477760/zeekrlife-mp-osp/20240415/b2c3900d7ab34fea9005a1c11384daa3.jpg",
                R.mipmap.ic_prize,
                R.mipmap.ic_hot
            )
            , null)


        findViewById<SpaceLineTabLayout>(R.id.tab_layout).setTabs(arrayListOf("tab1", "tab1", "tab1"))


        findViewById<TextView>(R.id.textview).let{
            it.setMovementMethod(object : LinkMovementMethod() {
                override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
                    val action = event.action
                    if (action == MotionEvent.ACTION_UP) {
                        var x = event.x.toInt()
                        var y = event.y.toInt()
                        x -= widget.totalPaddingLeft
                        y -= widget.totalPaddingTop
                        x += widget.scrollX
                        y += widget.scrollY
                        val layout: Layout = widget.layout
                        val line: Int = layout.getLineForVertical(y)
                        val off: Int = layout.getOffsetForHorizontal(line, x.toFloat())
                        val link = buffer.getSpans(off, off, ClickableSpan::class.java)
                        if (link.size != 0) {
                            link[0].onClick(widget)
                            return true
                        }
                    }
                    return super.onTouchEvent(widget, buffer, event)
                }
            });
            it.setText(Html.fromHtml("<a style=\"color: #ff8500;\" href=\"https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1449963274233501001&productType=1\" rel=\"noopener noreferrer\" target=\"_blank\">https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1449963274233501001&productType=1</a><span style=\"color: rgb(255, 133, 0);\">保险：</span><a style=\"color: #ff8500;\" href=\"https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1465914835880296458&productType=6\" rel=\"noopener noreferrer\" target=\"_blank\">https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1465914835880296458&productType=6</a><a style=\"color: #ff8500;\" href=\"https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1465914835880296458&productType=6\" rel=\"noopener noreferrer\" target=\"_blank\">测试</a><span style=\"color: rgb(255, 133, 0);\">还是说</span>服务商城：<a style=\"color: #ff8500;\" href=\"https://zeekrlife.com/pagesOrder/ServiceMallActivity\" rel=\"noopener noreferrer\" target=\"_blank\">https://zeekrlife.com/pagesOrder/ServiceMallActivity</a>尊享生产：<a style=\"color: #ff8500;\" href=\"https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1449963274233503001&productType=1\" rel=\"noopener noreferrer\" target=\"_blank\">https://app.zeekrlife.com/pagesOrder/CommodityDetailActivity?serverProductId=1449963274233503001&productType=1</a>"))
        }
    }

    //图层混合模式：https://blog.csdn.net/zb987570437/article/details/100529470
    fun getRoundedCornerBitmap(bitmap: Bitmap, topLeftRadius: Float, topRightRadius: Float, bottomRightRadius: Float, bottomLeftRadius: Float): Bitmap? {
        val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(output)
        //val color = -0xbdbdbe
        val paint = Paint()
        val rect = Rect(0, 0, bitmap.width, bitmap.height)
        val rectF = RectF(rect)
        paint.isAntiAlias = true
        //canvas.drawARGB(0, 0, 0, 0)
        //paint.color = color
        val path = Path()
        val radii =
            floatArrayOf(topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius)
        path.addRoundRect(rectF, radii, Path.Direction.CW)
        canvas.drawPath(path, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(bitmap, rect, rect, paint)
        return output
    }

    private fun navDragFloat(){
        startActivity(Intent(this, AnimTestActivity::class.java))
    }
}